home *** CD-ROM | disk | FTP | other *** search
- //: C24:Constcst.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // Const casts
-
- int main() {
- const int i = 0;
- int* j = (int*)&i; // Deprecated form
- j = const_cast<int*>(&i); // Preferred
- // Can't do simultaneous additional casting:
- //! long* l = const_cast<long*>(&i); // Error
- volatile int k = 0;
- int* u = const_cast<int*>(&k);
- }
-
- class X {
- int i;
- // mutable int i; // A better approach
- public:
- void f() const {
- // Casting away const-ness:
- (const_cast<X*>(this))->i = 1;
- }
- }; ///:~
-